package com.wsj.message_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsj.message_server.entity.*;
import com.wsj.message_server.mapper.MaterialCommentMapper;
import com.wsj.message_server.req.REQDeleteNotify;
import com.wsj.message_server.req.REQOfficiaNotifyInfo;
import com.wsj.message_server.req.REQPageInfo;
import com.wsj.message_server.service.NotifyService;
import com.wsj.message_server.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NotifyServicelmpl implements NotifyService {

    @Autowired
    private MaterialCommentMapper commentMapper;

    @Override
    public FFResponse<List<SystemNotify>> notifyList(Integer uid,  REQPageInfo data) {
        if (null==uid){
            return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", null, false,0);
        }
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        List<SystemNotify> list= commentMapper.systemNotify(uid);
        PageInfo<SystemNotify> pageInfo=new PageInfo<>(list);
        commentMapper.updateNotifyIsRead(uid);
        return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", pageInfo.getList(), pageInfo.isHasNextPage(),(int) pageInfo.getTotal());
    }

    @Override
    public FFResponse<List<CommentNotify>> commentList(Integer uid,  REQPageInfo data) {
        if (null==uid){
            return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", null, false,0);
        }
        List<CommentNotify> listall= commentMapper.commentNotify(uid);
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        List<CommentNotify> list= commentMapper.commentNotify(uid);
        PageInfo<CommentNotify> pageInfo = new PageInfo<>(list);
        if (listall.size()>0) {
            commentMapper.updateCommentIsRead(listall);
        }
        return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", pageInfo.getList(), pageInfo.isHasNextPage(),(int) pageInfo.getTotal());
    }

    @Override
    public FFResponse<List<FansNotify>> fansList(Integer uid,  REQPageInfo data) {
        if (null==uid){
            return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", null, false,0);
        }
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        List<FansNotify> list= commentMapper.fansList(uid);
        PageInfo<FansNotify> pageInfo=new PageInfo<>(list);
        commentMapper.updateFansIsRead(uid);
        return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", pageInfo.getList(), pageInfo.isHasNextPage(),(int) pageInfo.getTotal());
    }

    @Override
    public FFResponse<List<MaterialNotify>> materialList(Integer uid,  REQPageInfo data) {
        if (null==uid){
            return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", null, false,0);
        }
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        List<MaterialNotify> list= commentMapper.materialList(uid);
        PageInfo<MaterialNotify> pageInfo=new PageInfo<>(list);
        commentMapper.updateMateriaIsRead(uid);
        return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", pageInfo.getList(), pageInfo.isHasNextPage(),(int) pageInfo.getTotal());
    }

    @Override
    public FFResponse<NotifyNum> NotifyNum(Integer uid) {
        NotifyNum notifyNum=new NotifyNum();
        int officiaNum=commentMapper.officiaNum(uid);
        int systemNum=commentMapper.systemNum(uid);
        int commentNum=commentMapper.commentNum(uid);
        int fansNum=commentMapper.fansNum(uid);
        int materialNum=commentMapper.materialNum(uid);
        int orderNum=commentMapper.orderNum(uid);
        notifyNum.setCommentNum(commentNum);
        notifyNum.setFansNum(fansNum);
        notifyNum.setMaterialNum(materialNum);
        notifyNum.setSystemNum(systemNum);
        notifyNum.setOfficiaNum(officiaNum);
        notifyNum.setOrderNum(orderNum);
        List<SystemNotify> systemNotifyList= commentMapper.systemNotify(uid);
        if (systemNotifyList.size()>0){
            SystemNotify vo=systemNotifyList.get(0);
            notifyNum.setSystemFirstNews(vo.getNotifyContent());
        }else {
            notifyNum.setSystemFirstNews("暂时没有系统通知");
        }
        List<CommentNotify> commentNotifyList= commentMapper.commentNotify(uid);
        if (commentNotifyList.size()>0){
            CommentNotify vo = commentNotifyList.get(0);
            notifyNum.setCommenFirstNews(vo.getUserName()+"给你评论了");
        }else {
            notifyNum.setCommenFirstNews("暂时没有用户评论");
        }
        List<FansNotify> fansNotifyList= commentMapper.fansList(uid);
        if (fansNotifyList.size()>0){
            FansNotify vo = fansNotifyList.get(0);
            notifyNum.setFansFirstNews(vo.getFansName()+vo.getContent());
        }else {
            notifyNum.setFansFirstNews("暂时没有用户关注");
        }
        List<MaterialNotify> materialNotifyList= commentMapper.materialList(uid);
        if (materialNotifyList.size()>0){
            MaterialNotify vo = materialNotifyList.get(0);
            notifyNum.setMaterialFirstNews(vo.getUserName()+"给你点赞了");
        }else {
            notifyNum.setMaterialFirstNews("暂时没有用户点赞");
        }
        List<OfficiaNotify> officiaNotifyList= commentMapper.officiaList(uid);
        if (officiaNotifyList.size()>0){
            OfficiaNotify vo = officiaNotifyList.get(0);
            notifyNum.setOfficiaFirstNews(vo.getTitle());
        }else {
            notifyNum.setOfficiaFirstNews("暂时没有官方消息");
        }
        List<OrderNotify> orderNotifyList= commentMapper.orderNotify(uid);
        if (orderNotifyList.size()>0){
            OrderNotify vo = orderNotifyList.get(0);
            notifyNum.setOrdeyFirstNews(vo.getNotifyContent());
        }else {
            notifyNum.setOrdeyFirstNews("暂时没有订单消息");
        }
        return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", notifyNum, false,1);
    }

    @Override
    public FFResponse<String> delNotify(Integer uid, REQDeleteNotify data) {
        int row=0;
        switch (data.getType()){
            case 1:
                row=commentMapper.delSystemNotify(uid,data.getId());
                break;
            case 2:
                row=commentMapper.delCommentNotify(uid,data.getId());
                break;
            case 3:
                row=commentMapper.delFansNotify(uid,data.getId());
                break;
            case 4:
                row=commentMapper.delMaterialNotify(uid,data.getId());
                break;
            case 5:
                row=commentMapper.delOfficiaNotify(uid,data.getId());
                break;
        }
        if (row>0){
            return new FFResponse<>(ResponseUtil.querySuccessCode, "删除成功", null, false,1);
        }
        return new FFResponse<>(ResponseUtil.querySuccessCode, "删除失败", null, false,0);
    }

    /**
     * 官方通知列表
     * @param uid
     * @param data
     * @return
     */
    @Override
    public FFResponse<List<OfficiaNotify>> officiaNotify(Integer uid, REQPageInfo data) {
        if (null==uid){
            return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", null, false,0);
        }
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        List<OfficiaNotify> list= commentMapper.officiaNotify(uid);
        PageInfo<OfficiaNotify> pageInfo=new PageInfo<>(list);
        commentMapper.updateOfficiaIsRead(uid);
        return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", pageInfo.getList(), pageInfo.isHasNextPage(),(int) pageInfo.getTotal());
    }

    /**
     * 官方通知详情
     * @param uid
     * @param data
     * @return
     */
    @Override
    public FFResponse<OfficiaNotify> officiaNotifyInfo(Integer uid, REQOfficiaNotifyInfo data) {
        OfficiaNotify officiaNotify=commentMapper.officiaNotifyInfo(uid,data);
        return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", officiaNotify,false,1);
    }

    @Override
    public FFResponse<List<OrderNotify>> orderNotify(Integer uid, REQPageInfo data) {
        if (null==uid){
            return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", null, false,0);
        }
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        List<OrderNotify> list= commentMapper.orderNotify(uid);
        PageInfo<OrderNotify> pageInfo=new PageInfo<>(list);
        commentMapper.updateOrderIsRead(uid);
        return new FFResponse<>(ResponseUtil.querySuccessCode, "获取成功", pageInfo.getList(), pageInfo.isHasNextPage(),(int) pageInfo.getTotal());
    }
}
